package cdu.javaendwork.dao.impl;

import cdu.javaendwork.dao.Adoption_display_Dao;
import cdu.javaendwork.dao.BaseDao;
import cdu.javaendwork.model.utilModel.Adoption_display_info;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Adoption_display_DaoImpl  extends BaseDao implements Adoption_display_Dao{
    @Override
    public List<Adoption_display_info> getDisplayInfoByUserid(int id) {
        String sql = "SELECT p1.id , petid , r.userid , p1.status, r.date ,r.id from " +
                "(SELECT id , userid , petid , status , date  from table_post p where p.userid = "+ id +")" +
                " p1 left join table_request r on p1.id = r.postid;";
        List<Adoption_display_info> infos = new ArrayList<>();
        try {

            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();

            while(rs.next()){
                Adoption_display_info info = new Adoption_display_info();
                info.setId(rs.getInt("p1.id"));
                info.setPetid(rs.getInt("petid"));
                info.setUserid(rs.getInt("userid"));
                info.setStatus(rs.getInt("p1.status"));
                info.setDate(rs.getDate("r.date"));
                info.setRequestid(rs.getInt("r.id"));
                infos.add(info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(infos);
        return infos;
    }
}
